package com.spring.hx.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Result {
    private static final int SUCCESS = 200;
    private Map<String, Object> data;
    private String msg;
    private int code;

    private Result(String msg, int code) {
        this.msg = msg;
        this.code = code;
    }

    public static Result ok(int code, String msg) {
        return new Result(msg, code);
    }

    public static Result ok(String msg) {
        return new Result(msg, SUCCESS);
    }

    public static Result error(int code, String msg) {
        return new Result(msg, code).setData(null);
    }

    public static Result ofNullable(Object queryResult) {
        if (queryResult == null
                || queryResult instanceof List &&((List) queryResult).size() == 0
                || queryResult instanceof Map && ((Map) queryResult).entrySet().size() == 0 )
            return  ok("no result.") ;
        return  ok("").setData(queryResult);
    }

    private Result setData(Object obj) {
        this.data = new HashMap<>(3);
        data.put("data", obj);
        return this;
    }


}
